/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.field;

import com.inspur.edp.bef.bizentity.GspBizEntityElement;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.field.FieldGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.ElementAssoGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.ElementEnumGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.StringLiteral;

public class ChildFieldDefaultValueGenerator extends FieldGenerator {

  private String fieldName;
  private MediatePropertyInfo field;
  private EntityGeneratorContext dataTypeImpContext;

  public ChildFieldDefaultValueGenerator(String fieldName, MediatePropertyInfo field,
      EntityGeneratorContext dataTypeImpContext) {
    this.fieldName = fieldName;
    this.field = field;
    this.dataTypeImpContext = dataTypeImpContext;
  }

  @Override
  public Expression buildGetMethodBody() {
    if (field.getElement() instanceof GspBizEntityElement && field.getElement().getObjectType() != GspElementObjectType.Association) {
      switch (field.getElement().getMDataType()) {
        case Text:
        case String:
          StringLiteral stringLiteral = ast.newStringLiteral();
          stringLiteral.setLiteralValue("");
          return stringLiteral;
        case Integer:
          return ast.newNumberLiteral("0");
        case Boolean:
          return ast.newBooleanLiteral(false);
        case Decimal:
          addImport(BigDecimal.class.getTypeName());
          if(((GspBizEntityElement)field.getElement()).getBelongObject().getBelongModel().isSimplifyGen()) {
            //BigDecimal.ZERO
            return ast.newQualifiedName(ast.newSimpleName(BigDecimal.class.getSimpleName()),
                ast.newSimpleName("ZERO"));
          } else {
            //new BigDecimal("0.0")
            ClassInstanceCreation cls = ast.newClassInstanceCreation();
            cls.setType(new TypeInfo(BigDecimal.class).getType(ast));
            addImport(BigDecimal.class.getTypeName());
            StringLiteral stringLiteral1 = ast.newStringLiteral();
            stringLiteral1.setLiteralValue("0.0");
            cls.arguments().add(stringLiteral1);
            return cls;
          }
        default:
          return null;
      }

    } else {
      return null;
    }

  }

  @Override
  public String getFieldName() {
    return fieldName;
  }

  @Override
  protected TypeInfo getFieldType() {
    return field.getPropertyType();
  }

  public static TypeInfo getTypeInfoByField(IGspCommonField field,
      CefGeneratorContext dataTypeImpContext) {
    switch (field.getObjectType()) {
      case None:
        return Utils.getNativeType(field, dataTypeImpContext);
      case Enum:
        return new TypeInfo(ElementEnumGenerator.getName(field));
      case Association:

        return new TypeInfo(ElementAssoGenerator.getName(field));
      default:
        return null;
    }
  }


  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PRIVATE_KEYWORD);
    return list;
  }
}
